﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Dbs;
using 基于WPF的电缆采样数据分析系统.Models.Dbs.DbService;
using 基于WPF的电缆采样数据分析系统.MsgEvents.HistoricalData;
using 基于WPF的电缆采样数据分析系统.Service.Db;

namespace 基于WPF的电缆采样数据分析系统.ViewModels
{
    /// <summary>
    /// 历史数据区
    /// </summary>
    public class HistoricalDataUCViewModel : BindableBase
    {
        /// <summary>
        /// 发布订阅
        /// </summary>
        private readonly IEventAggregator Aggregator;

        /// <summary>
        /// 构造器
        /// </summary>
        public HistoricalDataUCViewModel()
        {
            Aggregator = App.GetService<IEventAggregator>();
            // 更新历史用户登录记录
            Aggregator.GetEvent<MsgHistoryUserLoginRecords>().Subscribe(UpdateHistoryUserLoginRecords);
            // 更新历史导入波形记录
            Aggregator.GetEvent<MsgHistoryImportWaveformRecords>().Subscribe(UpdateHistoryImportWaveformRecords);
            // 更新历史工程文件记录
            Aggregator.GetEvent<MsgHistoricalProjectDocumentation>().Subscribe(UpdateHistoricalProjectDocumentation);
            // 更新历史导出任务记录
            Aggregator.GetEvent<MsgHistoricalExportTaskRecords>().Subscribe(UpdateHistoricalExportTaskRecords);
            // 更新历史操作日志记录
            Aggregator.GetEvent<MsgHistoricalOperationLogsAreRecorded>().Subscribe(UpdateHistoricalOperationLogsAreRecorded);
            // 更新历史导出波形记录
            Aggregator.GetEvent<MsgHistoryExportWaveformRecords>().Subscribe(UpdateHistoryExportWaveformRecords);

            // 初始化历史数据信息
            InitHistoricalDataInfo();

            // 用户使用记录查询命令
            UserUsedQueryCmm = new DelegateCommand(UserUsedQueryFun);
        }

        /// <summary>
        /// 初始化历史数据信息
        /// </summary>
        private void InitHistoricalDataInfo()
        {
            HistoryUserLoginRecords = UserUsedRecordService.Instance.GetUserUsedRecords();
            HistoryImportWaveformRecords = ImportWavLogService.Instance.GetImportWavLogList();
            HistoricalProjectDocumentation = ProjectDataInfoDAOService.Instance.GetProjectDataInfoDAOList();
            HistoricalExportTaskRecords = TaskDataInfoDAOService.Instance.GetTaskDataInfoDAOList();
            HistoricalOperationLogsAreRecorded = AllUsedLogDAOService.Instance.GetAllUsedLogDAOList();
            HistoryExportWaveformRecords = UpdateWavListDAOService.Instance.GetUpdateWavListDAOList();

            TempHistoryUserLoginRecords = GetHistoryUserLoginRecords(HistoryUserLoginRecords);
        }


        #region 根据时间查询-历史用户登录记录

        /// <summary>
        /// 临时历史用户登录记录
        /// </summary>
        private ObservableCollection<UserUsedRecord> _TempHistoryUserLoginRecords;

        /// <summary>
        /// 临时历史用户登录记录
        /// </summary>
        public ObservableCollection<UserUsedRecord> TempHistoryUserLoginRecords
        {
            get { return _TempHistoryUserLoginRecords; }
            set
            {
                _TempHistoryUserLoginRecords = value;
                RaisePropertyChanged();
            }
        }


        private ObservableCollection<UserUsedRecord> GetHistoryUserLoginRecords(ObservableCollection<UserUsedRecord> userUsedRecordList)
        {
            ObservableCollection<UserUsedRecord> userUsedRecords = new ObservableCollection<UserUsedRecord>();

            for (int i = 0; i < userUsedRecordList.Count; i++)
            {
                userUsedRecords.Add(new UserUsedRecord()
                {
                    RId = userUsedRecordList[i].RId,
                    LoginDateTime = userUsedRecordList[i].LoginDateTime,
                    Uid = userUsedRecordList[i].Uid,
                });
            }

            return userUsedRecords;
        }


        /// <summary>
        /// 根据起始时间和截止时间获取用户登录记录
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="deadline"></param>
        private void GetHistoryUserLoginRecordsByTime(DateTime startTime, DateTime deadline)
        {
            TempHistoryUserLoginRecords = new ObservableCollection<UserUsedRecord>(HistoryUserLoginRecords.Where(r => r.LoginDateTime >= startTime && r.LoginDateTime <= deadline));

        }

        /// <summary>
        /// 用户使用起始日期
        /// </summary>
        private DateTime _UserUsedStartDate = DateTime.Now;

        /// <summary>
        /// 用户使用起始日期
        /// </summary>
        public DateTime UserUsedStartDate
        {
            get { return _UserUsedStartDate; }
            set { 
                _UserUsedStartDate = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 用户使用起始时间
        /// </summary>
        private string _UserUsedStartTime = "00:00:00";

        /// <summary>
        /// 用户使用起始时间
        /// </summary>
        public string UserUsedStartTime
        {
            get { return _UserUsedStartTime; }
            set { 
                _UserUsedStartTime = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 用户使用截止日期
        /// </summary>
        private DateTime _UserUsedDeadlineDate = DateTime.Now;

        /// <summary>
        /// 用户使用截止日期
        /// </summary>
        public DateTime UserUsedDeadlineDate
        {
            get { return _UserUsedDeadlineDate; }
            set { 
                _UserUsedDeadlineDate = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 用户使用截止时间
        /// </summary>
        private string _UserUsedDeadlineTime = "23:59:59";

        /// <summary>
        /// 用户使用截止时间
        /// </summary>
        public string UserUsedDeadlineTime
        {
            get { return _UserUsedDeadlineTime; }
            set { 
                _UserUsedDeadlineTime = value;
                RaisePropertyChanged();
            }
        }


        private DateTime _UserUsedStartDateTime;

        public DateTime UserUsedStartDateTime
        {
            get { return _UserUsedStartDateTime; }
            set { 
                _UserUsedStartDateTime = value;
                RaisePropertyChanged();
            }
        }

        private DateTime _UserUsedDeadlineDateTime;

        public DateTime UserUsedDeadlineDateTime
        {
            get { return _UserUsedDeadlineDateTime; }
            set { 
                _UserUsedDeadlineDateTime = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 根据时间更新用户使用记录
        /// </summary>
        private void UpdateUserUsedByTime()
        {
            string userUsedStartDateTime = string.Concat(UserUsedStartDate.ToString().Split(" ")[0], " ", UserUsedStartTime);
            UserUsedStartDateTime = DateTime.Parse(userUsedStartDateTime);
            UserUsedStartDate = UserUsedStartDateTime;
            UserUsedStartTime = UserUsedStartDateTime.TimeOfDay.ToString();

            string userUsedDeadlineDateTime = string.Concat(UserUsedDeadlineDate.ToString().Split(" ")[0], " ", UserUsedDeadlineTime);
            UserUsedDeadlineDateTime = DateTime.Parse(userUsedDeadlineDateTime);
            UserUsedDeadlineDate = UserUsedDeadlineDateTime;
            UserUsedDeadlineTime = UserUsedDeadlineDateTime.TimeOfDay.ToString();


            GetHistoryUserLoginRecordsByTime(UserUsedStartDateTime, UserUsedDeadlineDateTime);
        }

        /// <summary>
        /// 用户使用记录查询命令
        /// </summary>
        public DelegateCommand UserUsedQueryCmm { get; set; }

        /// <summary>
        /// 用户使用记录查询方法
        /// </summary>
        private void UserUsedQueryFun()
        {
            UpdateUserUsedByTime();

            IsQuery = true;
        }

        public bool IsQuery { get; set; } = false;

        #endregion


        #region 界面集合信息

        /// <summary>
        /// 历史用户登录记录
        /// </summary>
        private ObservableCollection<UserUsedRecord> _HistoryUserLoginRecords;

        /// <summary>
        /// 历史用户登录记录
        /// </summary>
        public ObservableCollection<UserUsedRecord> HistoryUserLoginRecords
        {
            get { return _HistoryUserLoginRecords; }
            set { 
                _HistoryUserLoginRecords = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 历史导入波形记录
        /// </summary>
        private ObservableCollection<ImportWavLog> _HistoryImportWaveformRecords;

        /// <summary>
        /// 历史导入波形记录
        /// </summary>
        public ObservableCollection<ImportWavLog> HistoryImportWaveformRecords
        {
            get { return _HistoryImportWaveformRecords; }
            set
            {
                _HistoryImportWaveformRecords = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 历史工程文件记录
        /// </summary>
        private ObservableCollection<ProjectDataInfoDAO> _HistoricalProjectDocumentation;

        /// <summary>
        /// 历史工程文件记录
        /// </summary>
        public ObservableCollection<ProjectDataInfoDAO> HistoricalProjectDocumentation
        {
            get { return _HistoricalProjectDocumentation; }
            set
            {
                _HistoricalProjectDocumentation = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 历史导出任务记录
        /// </summary>
        private ObservableCollection<TaskDataInfoDAO> _HistoricalExportTaskRecords;

        /// <summary>
        /// 历史导出任务记录
        /// </summary>
        public ObservableCollection<TaskDataInfoDAO> HistoricalExportTaskRecords
        {
            get { return _HistoricalExportTaskRecords; }
            set
            {
                _HistoricalExportTaskRecords = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 历史操作日志记录
        /// </summary>
        private ObservableCollection<AllUsedLogDAO> _HistoricalOperationLogsAreRecorded;

        /// <summary>
        /// 历史操作日志记录
        /// </summary>
        public ObservableCollection<AllUsedLogDAO> HistoricalOperationLogsAreRecorded
        {
            get { return _HistoricalOperationLogsAreRecorded; }
            set
            {
                _HistoricalOperationLogsAreRecorded = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 历史导出波形记录
        /// </summary>
        private ObservableCollection<UpdateWavListDAO> _HistoryExportWaveformRecords;

        /// <summary>
        /// 历史导出波形记录
        /// </summary>
        public ObservableCollection<UpdateWavListDAO> HistoryExportWaveformRecords
        {
            get { return _HistoryExportWaveformRecords; }
            set
            {
                _HistoryExportWaveformRecords = value;
                RaisePropertyChanged();
            }
        }


        #endregion


        #region 更新信息

        /// <summary>
        /// 更新历史用户登录记录
        /// </summary>
        private void UpdateHistoryUserLoginRecords()
        {
            if (IsQuery)
            {
                UserUsedQueryFun();
                return;
            }

            HistoryUserLoginRecords = UserUsedRecordService.Instance.GetUserUsedRecords();
            TempHistoryUserLoginRecords = HistoryUserLoginRecords;
        }

        /// <summary>
        /// 更新历史导入波形记录
        /// </summary>
        private void UpdateHistoryImportWaveformRecords()
        {
            HistoryImportWaveformRecords = ImportWavLogService.Instance.GetImportWavLogList();
        }

        /// <summary>
        /// 更新历史工程文件记录
        /// </summary>
        private void UpdateHistoricalProjectDocumentation()
        {
            HistoricalProjectDocumentation = ProjectDataInfoDAOService.Instance.GetProjectDataInfoDAOList();
        }

        /// <summary>
        /// 更新历史导出任务记录
        /// </summary>
        private void UpdateHistoricalExportTaskRecords()
        {
            HistoricalExportTaskRecords = TaskDataInfoDAOService.Instance.GetTaskDataInfoDAOList();
        }

        /// <summary>
        /// 更新历史操作日志记录
        /// </summary>
        private void UpdateHistoricalOperationLogsAreRecorded()
        {
            HistoricalOperationLogsAreRecorded = AllUsedLogDAOService.Instance.GetAllUsedLogDAOList();
        }

        /// <summary>
        /// 更新历史导出波形记录
        /// </summary>
        private void UpdateHistoryExportWaveformRecords()
        {
            HistoryExportWaveformRecords = UpdateWavListDAOService.Instance.GetUpdateWavListDAOList();
        }

        #endregion


    }
}
